import { createRouter,createWebHistory } from 'vue-router'

const routes = [
    {
        path:'/home',
        name:'home',
        component:() => import('../pages/Home.vue'),
        meta:{
            title:'首页'
        }
    },
    {
        path:'/login',
        name:'login',
        component:() => import('../pages/Login/Login.vue'),
        meta:{
            title:'登录页面'
        }
    },
    {
        path: '/problemList',
        name: 'problemList',
        component: () => import('../pages/ProblemList/problemList.vue'),
        meta:{
            title:'题目列表页面'
        }
    },
    {
        path: '/register',
        name: 'register',
        component: () => import('../pages/Login/register.vue'),
        meta:{
            title:'注册'
        }
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

const whilePath = ['/login','/register']
router.beforeEach((to,from,next) => {
    document.title = to.meta.title;
    if(!whilePath.includes(to.path)){//需要登录，不在whilePath中
        if(!localStorage.getItem('userInfo')){
          router.push('/login')
        }else{
         next()
        }
      }
      next()
      return
})

export default router